package colab.persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import colab.dominio.Predio;
import colab.dominio.Usuario;

public class PrediosDAO {
	
	
	public static ArrayList<Predio> listarPredios() throws SQLException{
		ArrayList<Predio> predios = new ArrayList<Predio>();
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		
		try {
			conn = Conexao.getConnection();
			stmt = conn.prepareStatement("select * from predios");
			rs = stmt.executeQuery();
			while(rs.next()){
				Predio predio = new Predio();
				predio.setId_predio(rs.getInt("Id"));
				predio.setNome(rs.getString("nome"));
				predios.add(predio);
				}
			
	        
			} 
		catch (SQLException e) {
			e.printStackTrace();
			}
		 finally {
			 
			 
			 	rs.close();
				stmt.close();
				conn.close();
			   }
		
		return predios;
		}
	
	public static Predio getPredio(String nomePredio) throws SQLException{
		Predio predio = null;
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		
		try {
			conn = Conexao.getConnection();
			stmt = conn.prepareStatement("select * from predios where nome='"+nomePredio+"'");
			rs = stmt.executeQuery();
			
			if(rs.next()){
				predio = new Predio();
				predio.setId_predio(rs.getInt("Id"));
				predio.setNome(rs.getString("nome"));
			}
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		 finally {
			
			 	rs.close();
				stmt.close();
				conn.close();
			   }
		
		return predio;
	}
	

}
